package com.example.controller;

// UserController.java

import com.example.entity.LoginRequest;
import com.example.mapper.AdminMapper;
import com.example.mapper.StudentMapper;
import com.example.service.StudentService;
import com.example.utils.jwtUtils;
import com.example.entity.Student;
import com.example.utils.Result;
import com.example.entity.Admin;
import com.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.http.ResponseEntity;
import org.springframework.http.HttpStatus;

import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
     private UserService userService;
    @Autowired
    private AdminMapper adminMapper;
    @Autowired
    private StudentService studentService;

    @Autowired
    private StudentMapper studentMapper;
   private String roles;

    @PostMapping("/login")
    public Result login(@RequestBody LoginRequest loginRequest) {
        String username = loginRequest.getUsername();
        String password = loginRequest.getPassword();
        roles = loginRequest.getRoles();
        if(roles.equals("student"))
        // 使用数据库查询逻辑验证用户信息
        {
            Student student = studentMapper.findAllByid1(username);
            if (student == null) {
                return Result.error("用户不存在");
            }
            if (!student.getPassword().equals(password)) {
                return Result.error("密码错误");
            }
        }else if(roles.equals("admin")){
            Admin admin = adminMapper.getAdminByUsername(username);
            if (admin == null) {
                return Result.error("用户不存在");
            }
            if (!admin.getPassword().equals(password)) {
                return Result.error("密码错误");
            }
        }else {
            if(username.equals("999") && password.equals("999")) {
                String token = jwtUtils.generateToken(loginRequest.getUsername());
                return Result.ok().data("token", token);
            }
            else {
                return Result.error("输入错误");
            }
        }
        String token = jwtUtils.generateToken(loginRequest.getUsername());
        return Result.ok().data("token", token);
    }
          @GetMapping("/info")
        public Result info (String token){
            String username = jwtUtils.getClaimsByToken(token).getSubject();
                if(roles.equals("student")) {
                    Student user = studentMapper.findAllByid1(username);
                    String name = user.getName();
                    String url = "https://tupian.qqw21.com/article/UploadPic/2020-8/20208522181570993.jpg";
                    return Result.ok()
                            .data("name", name)
                            .data("avatar", url)
                            .data("roles", roles)
                            .data("user", user);
                }else if (roles.equals("admin")){
                    Admin user = adminMapper.getAdminByUsername(username);
                    String name = user.getName();
                    String url = "https://tupian.qqw21.com/article/UploadPic/2020-8/20208522181570993.jpg";
                    return Result.ok()
                            .data("name", name)
                            .data("avatar", url)
                            .data("roles", roles)
                            .data("user", user);
                }
                else{
                    String name = "999";
                    String url = "https://tupian.qqw21.com/article/UploadPic/2020-8/20208522181570993.jpg";
                    roles = "superadmin";
                    return Result.ok()
                            .data("name", name)
                            .data("avatar", url)
                            .data("roles", roles);
//                            .data("user", user);
                }
                }
    @PostMapping("/logout")
    public Result logout() { return Result.ok(); }
    @PostMapping("/updatePassword")
    public Result changePassword(@RequestBody Map<String, String> request) {
        String token = request.get("token");
        String roles = request.get("roles");
        String oldPassword = request.get("oldPassword");
        String newPassword = request.get("newPassword");

        String username = jwtUtils.getClaimsByToken(token).getSubject();
        if (roles.equals("student")) {
            Student student = studentMapper.findAllByid1(username);
            if (student == null) {
                return Result.error("User not found");
            }
            if (!student.getPassword().equals(oldPassword)) {
                return Result.error("原始密码输入错误");
            }
            student.setPassword(newPassword);
            studentMapper.updatepassword(student); // Update the student's password in the database
        } else {
            Admin admin = adminMapper.getAdminByUsername(username);
            if (admin == null) {
                return Result.error("User not found");
            }
            if (!admin.getPassword().equals(oldPassword)) {
                return Result.error("原始密码输入错误");
            }
            admin.setPassword(newPassword);
            adminMapper.updatepassword(admin);
        }

        return Result.ok();
    }

}

